package com.google.demo.Activity;

import java.util.Observable;
import java.util.Observer;

import com.google.demo.Config;
import com.google.demo.R;
import com.google.demo.ScanQRCodeApp;
import com.google.demo.Data.Controler.DataHelper;
import com.google.demo.Util.Util;
import com.google.demo.model.DataResult;
import com.google.demo.model.UserInfo;
import com.google.demo.view.AnimValidation;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginMainActivity extends MainActivity implements Observer{
	//////Controls
	private Button mButton_login;
	private EditText mEditText_username,mEditText_password;
	private TextView mTextView_markUsername,mTextView_markPassword;
	/////Variables
	private LoginMainActivity me=this;
	private void initControls(){
		getmButton_left().setVisibility(View.GONE);
		getmButton_right().setVisibility(View.GONE);
		mButton_login=(Button) findViewById(R.id.btn_login);
		mButton_login.setOnClickListener(this.click_process());
		mEditText_username=(EditText) findViewById(R.id.edit_txtv_user_name);
		mEditText_password=(EditText) findViewById(R.id.edit_txtv_password);
		mTextView_markUsername=(TextView) findViewById(R.id.txtv_markUsername);
		mTextView_markPassword=(TextView) findViewById(R.id.txtv_markPassword);
		
		AnimValidation validation=new AnimValidation(me, 0, 
				mEditText_username, mEditText_password, 
				mTextView_markUsername, mTextView_markPassword);
		validation.validAnim();
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_main);
		initControls();
		
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		finish();
		android.os.Process.killProcess(android.os.Process.myPid());
		
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		DataHelper.getInstance().deleteObserver(me);
	}
	private OnClickListener click_process(){
		return new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				String username=mEditText_username.getText().toString().trim();
				String password=mEditText_password.getText().toString().trim();
				String devid=Util.getAndroidID(me);
				if (username.length()<5) {
					
					return;
				}
				if (password.length() < 5) {
					
					return;
				}
				if(Util.isNetworkAvailable()) {
					Config.mProgressDialog=ProgressDialog.show(me, "",
							getString(R.string.processing));
					DataHelper.getInstance().addObserver(me);
					DataHelper.getInstance().login(username, password, devid);
				} else {
					Util.showError(LoginMainActivity.this, getString(R.string.app_name), getString(R.string.NETWORK_ERROR));
				}
				
			}
		};
	}
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		if (data == null)
		{
			
			return;
		}
		DataResult result = (DataResult) data;
		switch (result.getMethodName()) {
		case login:
			UserInfo userInfo=(UserInfo) result.getData();
			if(userInfo.ismIsLogin()){
				checkResultLogin(result);
			}else{
				Util.showError(LoginMainActivity.this, getString(R.string.app_name), getString(R.string.mes_login_fail));
			}
			Config.mProgressDialog.dismiss();
			break;
		}
	}
	private void checkResultLogin(DataResult result) {
		if (result.getErrorCode() == null) {
			
			UserInfo userInfo = (UserInfo) result
					.getData();
			ScanQRCodeApp.Instance().saveLoginInfo(userInfo);
			
			if(userInfo.ismIsLogin()){
				
				ScanQRCodeApp.Instance().isFirstStartLoginActivity=false;
				Util.showMessage(me,getString(R.string.mes_login_success) , 2000);
				Intent intent=new Intent(me,InputDataActivity.class);
				me.startActivity(intent);
				finish();
			}
			else if(!ScanQRCodeApp.Instance().isFirstStartLoginActivity&&userInfo.ismIsLogin())
			{
				
				setResult(RESULT_OK);
				
				finish();
				
			}
		} else
			Util.showError(LoginMainActivity.this, getString(R.string.app_name), result.getErrorCode().getErrorMessage());
		
	}
	
	
}
